using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

public class CameraBehavior : MonoBehaviour {
    public PlayerComponent player;
    public float cameraViewRatio;
    public float cameraRatioSpeed;
    public Vector2 cameraRatioRange;
    // Start is called before the first frame update
    void Start() {

    }

    // Update is called once per frame
    void Update() {
        //鼠标滚轮调整摄像头大小
        float mouseScroll = Input.GetAxis("Mouse ScrollWheel");
        if (mouseScroll != 0) {
            cameraViewRatio += mouseScroll * 10f;
        }

        //限制最大最小
        cameraViewRatio = Math.Max(cameraRatioRange.x, cameraViewRatio);
        cameraViewRatio = Math.Min(cameraRatioRange.y, cameraViewRatio);
    }

    void Awake() {
        player = FindObjectOfType<PlayerComponent>();
    }

    //这个lateupdate是相机更新专用
    void LateUpdate() {
        float targetCameraHeight = -player.playerSize * cameraViewRatio * cameraRatioSpeed;
        if (player != null) {
            //镜头抬高加进来
            transform.position = new Vector3(
                player.transform.position.x,
                player.transform.position.y,
                (targetCameraHeight + transform.position.z * 10f) / (cameraRatioSpeed + 10f)
            );//缓动镜头（用了个加权平均）
            
            
        }
    }

}
